/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.chlitina.store.modules.sys.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.mapper.JsonMapper;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.sys.dao.StoreMenuDao;
import com.chlitina.store.modules.sys.entity.StoreMenu;
import com.chlitina.store.modules.sys.service.SystemService;
import com.chlitina.store.modules.sys.utils.StoreUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 菜单Controller
 * 
 * @author ThinkGem
 * @version 2013-3-23
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH + "/sys/menu")
public class MenuController extends BaseController {

	@Autowired
	private SystemService systemService;
	@Autowired
	private StoreMenuDao storeMenuDao;

	@ModelAttribute("menu")
	public StoreMenu get(@RequestParam(required = false) Long id) {
		if (id != null) {
			return systemService.getMenu(id);
		} else {
			return new StoreMenu();
		}
	}

	@RequestMapping(value = { "list", "" })
	public String list(StoreMenu menu, Model model) {
		List<StoreMenu> list = Lists.newArrayList();
		List<StoreMenu> sourcelist = storeMenuDao.findAllList();
		StoreMenu.sortList(list, sourcelist, 1L);
		model.addAttribute("list", list);

		model.addAttribute("menuEditable", StoreUtils.isEditable("菜单管理"));
		return "modules/sys/menuList";
	}

	@RequestMapping(value = "form")
	public String form(StoreMenu menu, Model model) {
		if (menu.getParent() == null || menu.getParent().getId() == null) {
			menu.setParent(new StoreMenu(1L));
		}
		menu.setParent(systemService.getMenu(menu.getParent().getId()));
		model.addAttribute("menu", menu);
		return "modules/sys/menuForm";
	}

	@RequestMapping(value = "save")
	public String save(StoreMenu menu, Model model,
			RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, menu)) {
			return form(menu, model);
		}
		systemService.saveMenu(menu);
		addMessage(redirectAttributes, "保存菜单'" + menu.getName() + "'成功");
		return "redirect:" + Global.ADMIN_PATH + "/sys/menu/";
	}

	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		if (StoreMenu.isRoot(id)) {
			addMessage(redirectAttributes, "删除菜单失败, 不允许删除顶级菜单或编号为空");
		} else {
			systemService.deleteMenu(id);
			addMessage(redirectAttributes, "删除菜单成功");
		}
		return "redirect:" + Global.ADMIN_PATH + "/sys/menu/";
	}

	@RequiresUser
	@ResponseBody
	@RequestMapping(value = "treeData")
	public String treeData(@RequestParam(required = false) Long extId,
			HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<StoreMenu> list = systemService.findAllMenu();
		for (int i = 0; i < list.size(); i++) {
			StoreMenu e = list.get(i);
			if (extId == null
					|| (extId != null && !extId.equals(e.getId()) && e
							.getParentIds().indexOf("," + extId + ",") == -1)) {
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParent() != null ? e.getParent().getId()
						: 0);
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return JsonMapper.getInstance().toJson(mapList);
	}
}
